<template>
  <el-dialog
    title="新增试卷"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="JADP-dialog JADP-dialog_center"
    lock-scroll
    width="70%"
    append-to-body
    @close="close"
  >
    <div class="JADP-common-layout" style="height: 45vh">
      <div class="JADP-common-layout-left" style="height: 100%">
        <div class="JADP-common-title">
          <h2>试卷分类</h2>
        </div>
        <el-tree
          :data="treeData"
          :props="defaultProps"
          default-expand-all
          highlight-current
          ref="treeBox"
          :expand-on-click-node="false"
          @node-click="handleNodeClick"
          class="JADP-common-el-tree"
          node-key="id"
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <i :class="data.icon"></i>
            <span class="text">{{ node.label }}</span>
          </span>
        </el-tree>
      </div>
      <div class="JADP-common-layout-center">
        <div class="JADP-common-layout-main JADP-flex-main">
          <el-row class="JADP-common-search-box" :gutter="16">
            <el-form @submit.native.prevent>
              <el-col :span="6">
                <el-form-item label="试卷名称">
                  <el-input
                    v-model="query.testpapername"
                    placeholder="请输入"
                    clearable
                  >
                  </el-input>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label="试卷类型">
                  <el-select
                    v-model="query.testpapertype"
                    placeholder=""
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in getDict('testpapertype')"
                      :key="index"
                      :label="item.dataName"
                      :value="item.id"
                      :disabled="item.disabled"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <!-- <el-col :span="6">
                <el-form-item label="发布状态">
                  <el-select
                    v-model="query.releasestatus"
                    placeholder="请选择"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in getDict('releasestatus')"
                      :key="index"
                      :label="item.dataName"
                      :value="item.id"
                      :disabled="item.disabled"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="6">
                <el-form-item>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="search()"
                    >查询</el-button
                  >
                  <el-button icon="el-icon-refresh-right" @click="reset()"
                    >重置</el-button
                  >
                </el-form-item>
              </el-col>
            </el-form>
          </el-row>
          <el-table
            ref="myTable"
            v-loading="listLoading"
            :data="list"
            row-key="id"
            border
            highlight-current-row
            @current-change="handleCurrentChange"
            height="600px"
          >
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column
              prop="testpapername"
              label="试卷名称"
              align="left"
            />
            <el-table-column label="分类" prop="classification" algin="left" />
            <el-table-column
              prop="testpapertype"
              label="试卷类型"
              align="left"
            />
            <el-table-column
              prop="questionmethod"
              label="出题方式"
              align="left"
            />
            <!-- <el-table-column
              prop="examinationstatus"
              label="考试状态"
              align="left"
            />
            <el-table-column
              prop="examinationresults"
              label="考试结果"
              align="left"
            /> -->
            <el-table-column
              prop="releasestatus"
              label="发布状态"
              align="left"
            />
            <el-table-column prop="totalScore" label="总分" align="left" />
          </el-table>
          <pagination
            class="pagination"
            :total="total"
            :page.sync="listQuery.currentPage"
            :limit.sync="listQuery.pageSize"
            :page-sizes="[5, 10, 20, 50]"
            @pagination="initData"
          />
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
export default {
  name: "aaa",
  props: [],
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "fullName"
      },
      list: [],
      treeData: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        sort: "desc",
        sidx: ""
      },
      query: {
        classification: undefined,
        testpapertype: undefined,
        questionmethod: undefined,
        testpapername: undefined
      },
      listLoading: false,
      visible: false,
      id: "",
      arrSelection: [],
      currentRow: undefined
    };
  },
  created() {
    this.getTreeData();
    this.initData();
  },

  methods: {
    handleNodeClick(data) {
      this.query.classification = data.id;
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      };
      this.initData();
    },
    getTreeData() {
      this.request({
        url: "/api/xy_ks_paper_type/Xy_ks_paper_type/getTreeList",
        method: "get"
      }).then(res => {
        let topItem = {
          fullName: "默认",
          hasChildren: true,
          id: "-1",
          icon: "icon-ym icon-ym-tree-organization3",
          children: res.data
        };
        this.treeData = [topItem];
      });
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        releasestatus: "6d5c2898dfb84a20a8afcb70fecef9ef",
        organizeId:this.$store.getters.organizeId
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      this.request({
        url: `/api/testPaperManage/Xy_ks_examination_paper`,
        method: "get",
        data: query
      }).then(res => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    // 表单提交
    dataFormSubmit() {
      if (!this.currentRow) return this.$message.warning("请选择一个试卷");
      this.$emit("currentChange", this.currentRow);
      this.visible = false;
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    // 查询
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      };
      this.initData();
    },
    //重置
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatortime"
      };
      this.initData();
    },
    // selected(row) {
    //   return !this.ParentList.some(item => item.sourse_id === row.id);
    // }
    close() {
      this.$emit("refresh", true);
    }
  }
};
</script>
